<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/2/24
 * Time: 16:17
 */
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
use Think\Think;
use Common\Model\OptionModel;

class FuturesUserSummaryController extends AdminController{
    /**
     * 资金汇总列表
     * */
    public function index(){
    	$qqlcd_Model = new OptionModel('order');			    //恒指订单
    	$ouyuan_model = new OptionModel('euro_order');		//欧元订单
      $gold_model = new OptionModel('gold_order');			//美元订单
      //判断是否有用户查询
    	if($_GET['id'] || $_GET['op']){
        $uid = $_GET['id'];						    //获取用户id
      	$rec['a.is_pay'] = 1;
      	$cash['a.is_pay'] = array('neq',2);
        if($_GET['op']){
          $rec['b.maker_id'] = array('neq',0);
          $cash['b.maker_id'] = array('neq',0);
          $cms['b.maker_id'] = array('neq',0);
          $odr['b.maker_id'] = array('neq',0);
          $sql['maker_id'] = array('neq',0);
          $member = M('follow')->where($sql)->select();
          foreach($member as $k=>$v){
            $routes[$k] = $v['id'];
          }
          $mongo_sql['user_id'] = array('in',$routes);
        }else{
          $mongo_sql['user_id'] = $uid;			//订单查询条件
          $odr['a.user_id'] = $uid;					  //螺纹钢
          $rec['a.user_id'] = $uid;					  //充值查询条件
          $cash['a.user_id'] = $uid;          //提现查询条件
          $cms['a.commission_user_id'] = $uid;		//佣金查询条件
          $uname = M('follow')->where('id='.$uid)->field('nickname,money')->find();		//获取用户名
        }
        //时间段搜索
        $stime = strtotime($_REQUEST['start_time']);
        $etime = strtotime($_REQUEST['end_time']);
        if($stime){
          $cms['a.create_time'] = array('egt',$stime);//佣金
          $odr['a.create_time']  = array('egt',$stime);//螺纹钢
          $rec['a.create_time'] = array('egt',$stime);//充值时间
          $cash['a.add_time'] = array('egt',$stime);	//提现时间
          $mongo_sql['start_time'] = array('$gte'=>$stime);	//下单时间
        }
        if($etime){
          $end_time = $etime+86400;
          $cms['_string'] = 'a.create_time <='.$end_time;//佣金
          $rec['_string'] = 'a.create_time <='.$end_time;//充值时间
          $cash['_string'] = 'a.add_time <='.$end_time;	//提现时间
          $odr['_string'] = 'a.create_time <='.$end_time;       //螺纹钢
          if(empty($stime)){
            $mongo_sql['start_time'] = array();
          }
          $mongo_sql['start_time'] += array('$lte'=>$end_time);	//下单时间
        }
      	//欧元下单数据
      	$oy_order = $ouyuan_model->where($mongo_sql)->field('invest,profit')->select();
        $profit['oy_order']['title'] = '欧元订单';
        foreach($oy_order as $v){
      		$profit['oy_order']['invest'] += $v['invest'];
          if($v['profit'] > 0){
        		$profit['oy_order']['profit'] += $v['profit']-$v['invest'];
        	}else{
        		$profit['oy_order']['loss'] += $v['profit'];
        	}
      	}
      	$profit['oy_order']['fee'] = $profit['oy_order']['invest']*0.02;
      	$profit['oy_order']['pal'] = -$profit['oy_order']['loss']-$profit['oy_order']['profit']-$profit['oy_order']['fee'];//计算公司盈亏
      	//恒指下单数据
      	$order = $qqlcd_Model->where($mongo_sql)->field('invest,profit')->select();
        $profit['hz_order']['title'] = '恒指订单';
        foreach($order as $v){
      		$profit['hz_order']['invest'] += $v['invest'];
          if($v['profit'] > 0){
        		$profit['hz_order']['profit'] += $v['profit']-$v['invest'];
        	}else{
        		$profit['hz_order']['loss'] += $v['profit'];
        	}
      	}
      	$profit['hz_order']['fee'] = $profit['hz_order']['invest']*0.02;
      	$profit['hz_order']['pal'] = -$profit['hz_order']['loss']-$profit['hz_order']['profit']-$profit['hz_order']['fee'];//计算公司盈亏
        //美元下单数据
        $order = $gold_model->where($mongo_sql)->field('invest,profit')->select();
        $profit['gold_order']['title'] = '美元订单';
        foreach($order as $v){
      		$profit['gold_order']['invest'] += $v['invest'];
          if($v['profit'] > 0){
        		$profit['gold_order']['profit'] += $v['profit']-$v['invest'];
        	}else{
        		$profit['gold_order']['loss'] += $v['profit'];
        	}
      	}
      	$profit['gold_order']['fee'] = $profit['gold_order']['invest']*0.02;
      	$profit['gold_order']['pal'] = -$profit['gold_order']['loss']-$profit['gold_order']['profit']-$profit['gold_order']['fee'];//计算公司盈亏
      	//螺纹钢订单数据
      	$order = M('Futures_order')->table('wp_futures_order as a')->join('wp_follow as b on a.user_id = b.id')->where($odr)->field('a.id,a.total_price,a.profit')->select();
        $profit['order']['title'] = '螺纹钢订单';
        foreach($order as $v){
      		$profit['order']['invest'] += $v['total_price'];
      		$income = $v['profit']-$v['total_price'];
          if($income > 0){
        		$profit['order']['profit'] += $income;
        	}else{
        		$profit['order']['loss'] += $income;
        	}
      	}
        $profit['order']['fee'] = $profit['order']['invest']*0.02;
      	$profit['order']['pal'] = -$profit['order']['loss']-$profit['order']['profit']-$profit['order']['fee'];//计算公司盈亏

      	//佣金数据
      	$commission = M('Futures_commission')->table('wp_futures_commission as a')->join('wp_follow as b on a.commission_user_id = b.id')->where($cms)->field('a.commission,a.status')->select();
        $com_data['already']['title'] =  '已分配';
        $com_data['not']['title'] =  '未分配';
        foreach($commission as $v){
      		if($v['status'] == 0){
      				$com_data['not']['commission'] += $v['commission'];
      		}elseif($v['status'] == 1){
      		    $com_data['already']['commission'] += $v['commission'];
      		}
      	}

      	//充值数据
      	$recharge = M('Futures_recharge')->table('wp_futures_recharge as a')->join('wp_follow as b on a.user_id = b.id')->where($rec)->field('a.money')->select();
        $gold['rec']['title'] = '充值';
        foreach($recharge as $v){
      		$gold['rec']['money'] +=	$v['money'];
      		$gold['rec']['fee'] += $v['money']*0.006;//充值手续费
      	}

      	//提现数据
      	$withdrawals = M('futures_withdrawals')->table('wp_futures_withdrawals as a')->join('wp_follow as b on a.user_id = b.id')->where($cash)->field('a.money,a.handling_fee')->select();
        $gold['cash']['title'] = '提现';
        foreach($withdrawals as $v){
      		$gold['cash']['money'] += $v['money'];
      		$gold['cash']['fee'] += $v['handling_fee'];//提现手续费
      	}

  	    //判断数据是否为空
        foreach($profit as $k => $v){
					$profit[$k]['invest'] = !empty($v['invest']) ? round($v['invest'],2) : 0;
					$profit[$k]['profit'] = !empty($v['profit']) ? round($v['profit'],2) : 0;
					$profit[$k]['loss'] = !empty($v['loss']) ? -round($v['loss'],2) : 0;
					$profit[$k]['pal'] = !empty($v['pal']) ? round($v['pal'],2) : 0;
          $profit[$k]['fee'] = !empty($v['fee']) ? round($v['fee'],2) : 0;
				}

        foreach ($com_data as $k => $v) {
					$com_data[$k]['commission'] = !empty($v['commission']) ? round($v['commission'],2) : 0;
				}

        foreach($gold as $k => $v){
					$gold[$k]['money'] = !empty($v['money']) ? round($v['money'],2) : 0;
					$gold[$k]['fee'] = !empty($v['fee']) ? round($v['fee'],2) : 0;
				}

  	    $this->assign('uname',$uname);
      	$this->assign('com_data',$com_data);
      	$this->assign('profit',$profit);
      	$this->assign('money',$money);
      	$this->assign('gold',$gold);
        $this->display('index');


      }else{


        //搜索
        $stime = strtotime($_REQUEST['start_time']);          //开始时间
        $etime = strtotime($_REQUEST['end_time']);            //结束时间
        $nickname = $_REQUEST['nickname'];                    //昵称查询
        if($stime){
          $cms['a.create_time'] = array('egt',$stime);          //佣金时间
          $u_cms['create_time'] = array('egt',$stime);          //佣金时间(用户资金)
          $rec['a.create_time'] = array('egt',$stime);             //充值时间
          $u_rec['create_time'] = array('egt',$stime);             //充值时间(用户资金)
          $cash['a.add_time'] = array('egt',$stime);	          //提现时间
          $u_cash['add_time'] = array('egt',$stime);	          //提现时间(用户资金)
          $odr['a.create_time']  = array('egt',$stime);         //螺纹钢
          $u_odr['create_time']  = array('egt',$stime);         //螺纹钢(用户资金)
          $mongo_sql['start_time'] = array('$gte'=>$stime);	    //下单时间
        }
        if($etime){
          $end_time = $etime+86400;
          $cms['_string'] = 'a.create_time <='.$end_time;       //佣金时间
          $u_cms['_string'] = 'create_time <='.$end_time;       //佣金时间(用户资金)
          $rec['_string'] = 'a.create_time <='.$end_time;          //充值时间
          $u_rec['_string'] = 'create_time <='.$end_time;          //充值时间(用户资金)
          $cash['_string'] = 'a.add_time <='.$end_time;	        //提现时间
          $u_cash['_string'] = 'add_time <='.$end_time;	        //提现时间(用户资金)
          $odr['_string'] = 'a.create_time <='.$end_time;       //螺纹钢
          $u_odr['_string'] = 'create_time <='.$end_time;       //螺纹钢(用户资金)
          if(empty($stime)){
            $mongo_sql['start_time'] = array();
          }
          $mongo_sql['start_time'] += array('$lte'=>$end_time);	//下单时间
        }
        if($nickname){
          $sql['nickname']=array('like',"%".trim($nickname)."%");
          $rec['b.nickname']=array('like',"%".trim($nickname)."%");
          $cash['b.nickname']=array('like',"%".trim($nickname)."%");
          $cms['b.nickname']=array('like',"%".trim($nickname)."%");
          $odr['b.nickname']=array('like',"%".trim($nickname)."%");
        }else{
          //maker_id
          $sql['maker_id'] = array('neq',0);

        }
        //资金汇总数据

        //统计条件
        $rec['a.is_pay'] = 1;             //充值成功
        $cash['a.is_pay'] = array('neq',2);//提现成功
        //maker_id
        $rec['b.maker_id'] = array('neq',0);
        $cash['b.maker_id'] = array('neq',0);
        $cms['b.maker_id'] = array('neq',0);
        $odr['b.maker_id'] = array('neq',0);
        $member = M('follow')->where($sql)->select();
        foreach($member as $k=>$v){
          $routes[$k] = $v['id'];
        }
        $mongo_sql['user_id'] = array('in',$routes);
        //恒指数据
        $mongo_order = $qqlcd_Model->where($mongo_sql)->field('invest')->select();
        if($mongo_order){
          foreach($mongo_order as $v){
            $profit['order']['sum'] += $v['invest'];
            //恒指盈亏
            $or_profit += $v['profit'];
            $or_income = $or_profit-$profit['order']['sum'];
          }
        }else{
          $profit['order']['sum'] = '';
        }
        //欧元数据
        $mongo_oy = $ouyuan_model->where($mongo_sql)->field('invest,profit')->select();
        if($mongo_oy){
          foreach($mongo_oy as $v){
            $profit['oy_order']['sum'] += $v['invest'];
            //欧元盈亏
            $oy_profit += $v['profit'];
            $oy_income = $oy_profit-$profit['oy_order']['sum'];
          }
        }else{
          $profit['oy_order']['sum'] = '';
        }
        //美元数据
        $mongo_gold = $gold_model->where($mongo_sql)->field('invest,profit')->select();
        if($mongo_gold){
          foreach($mongo_gold as $v){
            $profit['gold_order']['sum'] += $v['invest'];
            //美元盈亏
            $oy_profit += $v['profit'];
            $oy_income = $oy_profit-$profit['gold_order']['sum'];
          }
        }else{
          $profit['gold_order']['sum'] = '';
        }
        //充值数据
        $profit['rec'] = M('futures_recharge')->table('wp_futures_recharge as a')->join('wp_follow as b on a.user_id = b.id')->where($rec)->field('SUM(a.money) as sum')->find();
        //提现数据
        $profit['cash'] = M('futures_withdrawals')->table('wp_futures_withdrawals as a')->join('wp_follow as b on a.user_id = b.id')->where($cash)->field('SUM(a.money) as sum,SUM(a.handling_fee) as hsum')->find();
        //佣金数据
        $profit['cms'] = M('futures_commission')->table('wp_futures_commission as a')->join('wp_follow as b on a.commission_user_id = b.id')->where($cms)->field('SUM(a.commission) as sum')->find();
        //螺纹钢数据
        $profit['odr'] = M('futures_order')->table('wp_futures_order as a')->join('wp_follow as b on a.user_id = b.id')->where($odr)->field('SUM(a.total_price) as sum,SUM(a.profit) as psum')->find();
        //用户盈亏
        $profit['pal'] = $profit['odr']['psum']-$profit['odr']['sum']+$oy_income+$or_income;
        //充值手续费
        $profit['rec']['sxf'] = $profit['rec']['sum']*0.006;
        //公司盈亏
        $profit['corporate']['pal'] = -$profit['pal']+$profit['cash']['hsum']-$profit['rec']['sxf'];
        //默认值
        foreach($profit as $k=>$v){
          $profit[$k]['sum'] = !empty($profit[$k]['sum']) ? round($profit[$k]['sum'],2) : 0;
        }
        $profit['rec']['sxf'] = !empty($profit['rec']['sxf']) ? round($profit['rec']['sxf'],2):0;
        $profit['pal'] = !empty($profit['pal']) ? round($profit['pal'],2):0;
        $profit['cash']['hsum'] = !empty($profit['cash']['hsum']) ? round($profit['cash']['hsum'],2):0;
        $profit['corporate']['pal'] = !empty($profit['corporate']['pal']) ? round($profit['corporate']['pal'],2):0;

        //用户资金汇总



        //分页
        if(!$_GET['sort']){
          $count      = M('follow')->where($sql)->count();// 查询满足要求的总记录数
          $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
          $show       = $Page->show();// 分页显示输出
          //获取用户
          $user_data = M('follow')->where($sql)->limit($Page->firstRow.','.$Page->listRows)->field('id,nickname')->select();
        }else{
          //获取用户
          $user_data = M('follow')->where($sql)->field('id,nickname')->order('money desc')->select();
        }



        //查询用户资金
        foreach($user_data as $k=>$v){
          $oy_income = 0;
          $or_income = 0;
          $gold_income = 0;
          //用户id
          $id = $v['id'];
          //统计条件
          $u_rec['user_id'] = $id;             //充值用户
          $u_rec['is_pay'] = 1;
          $u_cash['user_id'] = $id;            //提现用户
          $u_cash['is_pay'] = array('neq',2);
          $u_cms['commission_user_id'] = $id;  //分佣者
          $u_odr['user_id'] = $id;             //下单者
          $mongo_sql['user_id'] = $id;    //mongodb
          //恒指数据
          $user_morder = $qqlcd_Model->where($mongo_sql)->field('invest,profit')->select();
          foreach($user_morder as $vo){
            $user_data[$k]['order'] += $vo['invest'];
            //恒指盈亏
            if($vo['profit'] > 0){
          		$or_income += $vo['profit']-$vo['invest'];
          	}else{
          		$or_income += $vo['profit'];
          	}
          }
          $user_data[$k]['order'] = !empty($user_data[$k]['order']) ? round($user_data[$k]['order'],2) : 0;           //默认值
          //欧元数据
          $user_moy = $ouyuan_model->where($mongo_sql)->field('invest,profit')->select();
          foreach($user_moy as $vo){
            $user_data[$k]['oyorder'] += $vo['invest'];
            //欧元盈亏
            if($vo['profit'] > 0){
          	  $oy_income += $vo['profit']-$vo['invest'];
          	}else{
          		$oy_income += $vo['profit'];
          	}
          }
          $user_data[$k]['oyorder'] = !empty($user_data[$k]['oyorder']) ? round($user_data[$k]['oyorder'],2) : 0;     //默认值
          //美元数据
          $user_gold = $gold_model->where($mongo_sql)->field('invest,profit')->select();
          foreach($user_gold as $vo){
            $user_data[$k]['goldorder'] += $vo['invest'];
            //美元盈亏
            if($vo['profit'] > 0){
          	  $gold_income += $vo['profit']-$vo['invest'];
          	}else{
          		$gold_income += $vo['profit'];
          	}
          }
          $user_data[$k]['goldorder'] = !empty($user_data[$k]['goldorder']) ? round($user_data[$k]['goldorder'],2) : 0;     //默认值
          //充值数据
          $rec_sum = M('Futures_recharge')->where($u_rec)->field('SUM(money) as sum')->find();
          $rec_sum['sum'] = !empty($rec_sum['sum']) ? round($rec_sum['sum'],2) : 0;           //默认值
          $user_data[$k]['rec'] = $rec_sum['sum'];
          $user_data[$k]['sxf'] = $rec_sum['sum']*0.006;
          $user_data[$k]['sxf'] = !empty($user_data[$k]['sxf']) ? round($user_data[$k]['sxf'],2) : 0;           //默认值
          //提现数据
          $cash_sum = M('futures_withdrawals')->where($u_cash)->field('SUM(money) as sum,SUM(handling_fee) as hsum')->find();
          $cash_sum['sum'] = !empty($cash_sum['sum']) ? round($cash_sum['sum'],2) : 0;          //默认值
          $cash_sum['hsum'] = !empty($cash_sum['hsum']) ? round($cash_sum['hsum'],2) : 0;       //默认值
          $user_data[$k]['cash'] = $cash_sum['sum'];
          $user_data[$k]['hsum'] = $cash_sum['hsum'];
          //佣金数据
          $cms_sum = M('futures_commission')->where($u_cms)->field('SUM(commission) as sum')->find();
          $cms_sum['sum'] = !empty($cms_sum['sum']) ? round($cms_sum['sum'],2) : 0;          //默认值
          $user_data[$k]['cms'] = $cms_sum['sum'];
          //螺纹钢数据
          $odr_sum = M('futures_order')->where($u_odr)->order($sort_str)->field('SUM(total_price) as sum,SUM(profit) as psum')->find();
          $odr_income = $odr_sum['psum']-$odr_sum['sum'];
          $odr_sum['sum'] = !empty($odr_sum['sum']) ? round($odr_sum['sum'],2) : 0;          //默认值
          $user_data[$k]['odr'] = $odr_sum['sum'];
          //用户盈亏
          $user_data[$k]['pal'] = $odr_income+$oy_income+$or_income+$gold_income;
          $user_data[$k]['pal'] = !empty($user_data[$k]['pal']) ? round($user_data[$k]['pal'],2) : 0;           //默认值
        }

        //点击排序
        if($_GET['sort']){
          $sort = $_GET['sort'];
          $sort_s = $_GET['sort_s'];
          $user_data = $this->sort($user_data,$sort,$sort_s);
        }
        //排序后分页
        if($_GET['sort']){
          $p = !empty($_GET['p']) ? $_GET['p'] : 1;
          $sort_p = ($p-1)*20;
          $count      = M('follow')->where($sql)->count();// 查询满足要求的总记录数
          $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
          $show       = $Page->show();// 分页显示输出
          //获取数组前20个
          $user_data = array_slice($user_data,$sort_p,20);
        }
        $this->assign('show',$show);
        $this->assign('user_data',$user_data);
        $this->assign('profit',$profit);
        $this->display('list');
      }
    }
    public function sort($array,$value,$sort_s){
      if($sort_s == 1){
        $sort = SORT_DESC;
      }elseif($sort_s == 2){
        $sort = SORT_ASC;
      }
      foreach($array as $v){
      	$vals[] = $v[$value];
      }
      array_multisort($vals,$sort,$array);
      return $array;
    }
}
